package com.szholly.utils.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
 * ����Map���������
 */
public class MapUtils<K, V> {
	/**
	 * �����������map��keyֵ
	 * 
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static <K, V> String[] getKeys(Map<K, V> map) {
		Iterator ite = map.entrySet().iterator();
		String[] keys = new String[map.size()];
		int i = 0;
		while (ite.hasNext()) {
			Map.Entry entry = (Map.Entry) ite.next();
			if (entry.getKey() instanceof String) {
				keys[i] = (String) entry.getKey();
			} else {
				keys[i] = "" + entry.getKey();
			}
			i++;
		}
		return keys;

	}

	/**
	 * �����������map��Valueֵ
	 * 
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static <K, V> String[] getValues(Map<K, V> map) {
		Iterator ite = map.entrySet().iterator();
		String[] keys = new String[map.size()];
		int i = 0;
		while (ite.hasNext()) {
			Map.Entry entry = (Map.Entry) ite.next();
			if (entry.getKey() instanceof String) {
				keys[i] = (String) entry.getValue();
			} else {
				keys[i] = "" + entry.getValue();
			}
			i++;
		}
		return keys;

	}

	/**
	 * ���Map���ض�value��keyֵ
	 * 
	 * @param map
	 * @param value
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static <K, V> K getMapKeyFromValue(Map<K, V> map, V value) {
		Set set = map.keySet();
		K key = null;
		Iterator it = set.iterator();
		while (it.hasNext()) {
			key = (K) it.next();
			if (value.equals(map.get(key))) {
				return key;
			}
		}
		throw new NullPointerException("û�ж�Ӧ��Keyֵ");
	}

	/**
	 * Map�е�key��Сд���д
	 */
	public static Map<String, Object> getMapKeyUpper(Map<String, Object> map) {
		Set<String> keys = map.keySet();
		Map<String, Object> newMap = new HashMap<String, Object>();
		String upperKey = "";
		for (String key : keys) {
			upperKey = key.toUpperCase();
			Object value = map.get(key);
			newMap.put(upperKey, value);
		}
		return newMap;
	}
}
